import { defineStore } from 'pinia';
import { login } from '@/api/login'; // 假设已存在登录API

export const useLoginStore = defineStore('login', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: JSON.parse(localStorage.getItem('userInfo')) || null
  }),
  getters: {
    isLoggedIn: (state) => !!state.token
  },
  actions: {
    async login(formData) {
      try {
        const { data } = await login(formData); // 调用后端接口
        localStorage.setItem('token', data.token);
        localStorage.setItem('userInfo', JSON.stringify(data.user));
        return true;
      } catch (error) {
        console.error('登录失败:', error);
        return false;
      }
    },
    logout() {
      localStorage.removeItem('token');
      localStorage.removeItem('userInfo');
    }
  }
});